﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using VAdvantage.Panels;

namespace VAdvantage.Controls
{
    public class VTabItem:TabItem,IDisposable
    {
        private TextBlock _text;
        private Image _disableIcon;
        private Image _Icon;

        public event MouseButtonEventHandler Selecting;


        public VTabItem()
        {
            this.IsEnabledChanged += OnEnabledChanged;
            _text = new TextBlock() { Text = "Item"  };

            _disableIcon = new Image() { Width = 8, Height = 8, VerticalAlignment = System.Windows.VerticalAlignment.Top, HorizontalAlignment = System.Windows.HorizontalAlignment.Right, Visibility = System.Windows.Visibility.Collapsed, Source = Utility.Envs.LoadImageSource("cross16.png") };

            _Icon = new Image() { Width = 16, Height = 16, VerticalAlignment = System.Windows.VerticalAlignment.Center, HorizontalAlignment = System.Windows.HorizontalAlignment.Center, Visibility = System.Windows.Visibility.Collapsed };
            
            StackPanel spHeader = new StackPanel() { Orientation = Orientation.Horizontal};
            
            spHeader.Children.Add(_Icon);
            spHeader.Children.Add(_text);
            spHeader.Children.Add(_disableIcon);

             base.Header = spHeader;
            
        }

        void OnEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                _disableIcon.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                _disableIcon.Visibility = System.Windows.Visibility.Visible;
            }
        }

        #region "Propety"

        public string Text
        {
            get
            {
                return _text.Text;
            }
            set
            {
                _text.Text = value;
            }
        }

        public ImageSource DisableIcon
        {
            set { _disableIcon.Source = value; }
        }

        public ImageSource Icon
        {
            set
            {
                _Icon.Source = value;
                if (value is BitmapImage)
                {
                    _Icon.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    _Icon.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        public int Index
        {
            get;
            set;
        }


        #endregion

        public void Dispose()
        {
            _text = null;
            _disableIcon = null;
            _Icon = null;
            base.Header = null;

            Selecting = null;

            if (this.Content is GridController)
            {
                ((GridController)this.Content).Dispose();
            }
            this.Content = null;
        }



        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (Selecting != null)
            {
                Selecting(this, e);
            }
            base.OnMouseLeftButtonDown(e);
        }
    }

}
